import request from '@/utils/request'

// 查询编码规则列表
export function listCoding(query) {
  return request({
    url: '/system/coding/list',
    method: 'get',
    params: query
  })
}

// 查询编码规则详细
export function getCoding(ruleId) {
  return request({
    url: '/system/coding/' + ruleId,
    method: 'get'
  })
}

// 新增编码规则
export function addCoding(data) {
  return request({
    url: '/system/coding',
    method: 'post',
    data: data
  })
}

// 修改编码规则
export function updateCoding(data) {
  return request({
    url: '/system/coding',
    method: 'put',
    data: data
  })
}

// 删除编码规则
export function delCoding(ruleId) {
  return request({
    url: '/system/coding/' + ruleId,
    method: 'delete'
  })
}

// 生成编码预览
export function previewCoding(data) {
  return request({
    url: '/system/coding/preview',
    method: 'post',
    data: data
  })
}

// 生成编码（临时编码，不更新流水号）
export function generateCode(tableName, fieldName, params = {}) {
  return request({
    url: '/system/coding/generate',
    method: 'post',
    headers: {
      repeatSubmit: false
    },
    data: {
      tableName,
      fieldName,
      ...params
    }
  })
}

// 确认编码（正式生成编码并更新流水号）
export function confirmCode(tableName, fieldName, params = {}) {
  return request({
    url: '/system/coding/confirm',
    method: 'post',
    headers: {
      repeatSubmit: false
    },
    data: {
      tableName,
      fieldName,
      ...params
    }
  })
}

// 获取可用的表列表（用于绑定表名选择）
export function getAvailableTables() {
  return request({
    url: '/system/coding/tables',
    method: 'get'
  })
}

// 根据表名获取字段列表
export function getTableFields(tableName) {
  return request({
    url: '/system/coding/fields/' + tableName,
    method: 'get'
  })
}

// 测试编码规则
export function testCodingRule(data) {
  return request({
    url: '/system/coding/test',
    method: 'post',
    data: data
  })
}

// 重置流水号
export function resetSerial(ruleId, segmentId) {
  return request({
    url: '/system/coding/reset/' + ruleId + '/' + segmentId,
    method: 'post'
  })
}

// 批量生成编码
export function batchGenerateCode(data) {
  return request({
    url: '/system/coding/batch',
    method: 'post',
    data: data
  })
}

// 导出编码规则
export function exportCoding(query) {
  return request({
    url: '/system/coding/export',
    method: 'get',
    params: query
  })
}

// 导入编码规则
export function importCoding(data) {
  return request({
    url: '/system/coding/import',
    method: 'post',
    data: data
  })
}

// 获取编码规则统计信息
export function getCodingStats() {
  return request({
    url: '/system/coding/stats',
    method: 'get'
  })
}

// 验证编码规则配置
export function validateCodingRule(data) {
  return request({
    url: '/system/coding/validate',
    method: 'post',
    data: data
  })
}

// 复制编码规则
export function copyCodingRule(ruleId, newRuleName) {
  return request({
    url: '/system/coding/copy/' + ruleId,
    method: 'post',
    data: {
      newRuleName: newRuleName
    }
  })
}

// 启用/禁用编码规则
export function toggleCodingStatus(ruleId, status) {
  return request({
    url: '/system/coding/status/' + ruleId + '/' + status,
    method: 'put'
  })
}